'''
#1. 内存空间占用
import numpy as np
from scipy import sparse

data = np.random.uniform(low = 0,high = 1,size=(10000,10000))
data[data<0.8]=0
print(data.nbytes/1024**2)

data_csr = sparse.csr_matrix(data)
sizes=(data_csr.data.nbytes+data_csr.indices.nbytes+data_csr.indptr.nbytes)/1024**2
print(sizes)

# 可视化
import numpy as np
import matplotlib.pyplot as plt

data = np.random.uniform(0,1,(20,60))
data[data<0.8] = 0
data[data>0.8] = (data[data>0.8]-0.8)/0.2
plt.imshow(data,cmap = 'hot')
plt.axis('off')
plt.show()
'''
# 稀疏矩阵运算
from scipy.sparse import lil_matrix
from scipy.sparse.linalg import spsolve
from numpy.linalg import solve ,norm
from numpy.random import rand
A = lil_matrix((1000,1000))
A[0,:100] = rand(100)
A[1,100:200] = A[0,:100]
A.setdiag(rand(1000))

A = A.tocsr()
b = rand(1000)
x = spsolve(A,b)
x_ = solve(A.toarray(),b)
err = norm(x-x_)
print(err < 1e-10)
